﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DuJianliang.ApplicationPlatform.Common;
using DuJianliang.ApplicationPlatform.Model;

namespace DuJianliang.ApplicationPlatform.TempEquipment
{
    public partial class TempEquipmentEdit : System.Web.UI.Page
    {
        TempEquipmentDal tempEquipmentDal = new TempEquipmentDal();

        public string FlowInstanceID
        {
            set
            {
                ViewState["FlowInstanceID"] = value;
            }
            get
            {
                return ViewState["FlowInstanceID"] != null ? ViewState["FlowInstanceID"].ToString() : string.Empty;
            }
        }

        public string Source
        {
            set
            {
                ViewState["Source"] = value;
            }
            get
            {
                return ViewState["Source"] != null ? ViewState["Source"].ToString() : string.Empty;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.FlowInstanceID = Request.QueryString["FlowInstanceID"];
                this.Source = Request.QueryString["source"];

                InitControlData();
                SetControlState();
            }
        }

        private void InitControlData()
        {
            if (string.IsNullOrWhiteSpace(this.FlowInstanceID))
            {
                lbl_Code.Text = tempEquipmentDal.CreateApplyCode();
                txt_applyDepartment.Text = Common.CurrentInfo.Instance.CurrentUser.Dept.DeptFullName;
                txt_applyPerson.Text = Common.CurrentInfo.Instance.CurrentUser.UserName;
            }
            else
            {
                Model.TempEquipmentOB obj = tempEquipmentDal.getTempEquipment(this.FlowInstanceID);
                lbl_Code.Text = obj.applyCode;
                txt_applyDepartment.Text = obj.applyDepartment;
                txt_applyPerson.Text = obj.applyPerson;
                txt_applyReason.Text = obj.applyReason;
                txt_factDate.Text = Common.Helper.Instance.ConvertToShotDate(obj.factDate);
                txt_needDate.Text = Common.Helper.Instance.ConvertToShotDate(obj.needDate);
                txt_usedFor.Text = obj.usedFor;
                BindChildGrid(this.FlowInstanceID);
            }
        }

        private void SetControlState()
        {
            bool hasFlowData = false;
            bool flowCompleted = false;
            bool currentFlowStepIsFirstSetp = false;
            string flowEndOperation = string.Empty;

            if (!this.Source.ToLower().Equals("list"))
            {
                txt_applyReason.Enabled = false;
                txt_factDate.Enabled = false;
                txt_needDate.Enabled = false;
                txt_usedFor.Enabled = false;
                btn_Add.Visible = false;
                btn_Back.Visible = false;
                btnOK.Visible = false;
                grv.Columns[5].Visible = false;
                return;
            }

            if (!string.IsNullOrWhiteSpace(this.FlowInstanceID))
            {
                btn_Add.Attributes.Clear();
                string url = "TempEquipmentDetailEdit.aspx?ParentID=" + this.FlowInstanceID;
                btn_Add.Attributes.Add("onclick", string.Format("showPage('{0}','500px','350px');", url));
                btn_Add.Visible = true;

                DataTable flowInstanceData = FlowHelper.GetFlowInstanceInfo(new Guid(this.FlowInstanceID));
                if (flowInstanceData != null && flowInstanceData.Rows.Count > 0)
                {
                    currentFlowStepIsFirstSetp = FlowHelper.CurrentFlowStepIsFirstSetp(flowInstanceData.Rows[0]["FlowID"].ToString()
                        , this.FlowInstanceID);

                    hasFlowData = true;

                    if (flowInstanceData.Rows[0]["Completed"] != null)
                    {
                        string strCompleted = flowInstanceData.Rows[0]["Completed"].ToString();

                        if (bool.TryParse(strCompleted, out flowCompleted) && flowCompleted)
                        {
                            if (flowInstanceData.Rows[0]["OperationID"] != null)
                            {
                                flowEndOperation = flowInstanceData.Rows[0]["OperationID"].ToString();
                            }
                        }
                    }
                }

                if ((hasFlowData && !currentFlowStepIsFirstSetp))
                {
                    txt_applyReason.Enabled = false;
                    txt_factDate.Enabled = false;
                    txt_needDate.Enabled = false;
                    txt_usedFor.Enabled = false;
                    btn_Add.Visible = false;
                    btnOK.Visible = false;
                    grv.Columns[5].Visible = false;
                }
                else
                {
                    txt_applyReason.Enabled = true;
                    txt_factDate.Enabled = true;
                    txt_needDate.Enabled = true;
                    txt_usedFor.Enabled = true;
                    btn_Add.Visible = true;
                    btnOK.Visible = true;
                    grv.Columns[5].Visible = true;
                }

                // 确定按钮
                if ((hasFlowData && !currentFlowStepIsFirstSetp))
                {
                    btnOK.Visible = false;
                }
                else
                {
                    btnOK.Visible = true;
                }

                if ((hasFlowData && !currentFlowStepIsFirstSetp))
                {
                    btn_Add.Visible = false;
                }
                else
                {
                    btn_Add.Visible = true;
                }
            }
            else
            {
                btn_Add.Visible = false;
            }            
        }

        private void BindChildGrid(string ParentID)
        {
            string condition = "ParentID='" + ParentID + "'";
            var dal = new TempEquipmentDal();

            List<Model.TempEquipmentDetialOB> list = dal.getTempEquipmentDetialList(condition);

            grv.DataSource = list;
            grv.DataBind();
        }

        protected void btn_Back_Click(object sender, EventArgs e)
        {
            Response.Redirect("TempEquipmentList.aspx");
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            string applyCode = lbl_Code.Text.Trim();
            string applyDepartment = txt_applyDepartment.Text.Trim();
            string applyPerson = txt_applyPerson.Text.Trim();
            string usedFor = txt_usedFor.Text.Trim();
            string needDate = txt_needDate.Text.Trim();
            string factDate = txt_factDate.Text.Trim();
            string applyReason = txt_applyReason.Text.Trim();

            Model.TempEquipmentOB obj = new Model.TempEquipmentOB();          
            obj.applyCode = applyCode;
            obj.applyDepartment = applyDepartment;
            obj.applyPerson = applyPerson;
            obj.factDate = factDate;
            obj.needDate = needDate;
            obj.usedFor = usedFor;
            obj.applyReason = applyReason;
            
             
            if (!string.IsNullOrWhiteSpace(this.FlowInstanceID))
            {
                obj.FlowInstanceID = this.FlowInstanceID;
                tempEquipmentDal.TempEquipment_Update(obj);
            }
            else
            {
                obj.FlowInstanceID = Guid.NewGuid().ToString();
                tempEquipmentDal.TempEquipment_Insert(obj);
                this.FlowInstanceID = obj.FlowInstanceID;
            }

            SetControlState();
        }
        
        protected void grv_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            var dal = new TempEquipmentDal();
            switch (e.CommandName)
            {
                case "CmdEdit" :
                    BindChildGrid(this.FlowInstanceID);
                    break;
                case "del":
                    dal.TempEquipmentDetial_Delete(e.CommandArgument.ToString());
                    BindChildGrid(this.FlowInstanceID);
                    break;
            }
        }

        protected void grv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Model.TempEquipmentDetialOB obj = (Model.TempEquipmentDetialOB)e.Row.DataItem;

                Button btnEdit = e.Row.FindControl("btnEdit") as Button;

                string url = "TempEquipmentDetailEdit.aspx?ID="+obj.ID+"&ParentID=" + this.FlowInstanceID;
                btnEdit.Attributes.Add("onclick", string.Format("showPage('{0}','500px','350px');", url));                
            }
        }

        protected void btn_Add_Click(object sender, EventArgs e)
        {
            BindChildGrid(this.FlowInstanceID);
        }
    }
}